package org.marverse.scripting

import ScriptingUtil._

sealed abstract case class UncheckedMemberAccessExpr(val base: UncheckedExpression, val name: Symbol)
extends UncheckedExpression {

}

object UncheckedMemberAccessExpr {
  def apply(base: UncheckedExpression, name: Symbol): UncheckedMemberAccessExpr =
    new UncheckedMemberAccessExprImpl(base, name)
}

private class UncheckedMemberAccessExprImpl(base: UncheckedExpression, name: Symbol) 
extends UncheckedMemberAccessExpr(base, name) {
  
  override def childUncheckedExpressions = Seq(base)
  override def checked(implicit ctx: MutableTypedMap[TypedKey, Any]) = {
    val b = base.checked
    b.expressionType.members.get(name) match {
      case None => new MemberNotFoundExpr(b.expressionType, name)
      case Some(m) => UncheckedInvocationExpr(UncheckedLiteralExpr(m, FunctionType(m.baseType, m.memberType)), UncheckedWrapperExpr(b)).checked
    }
  }

}

case class UncheckedWrapperExpr(val base: Expression) extends UncheckedExpression {
  def childUncheckedExpressions = Seq.empty
  def checked(implicit ctx: MutableTypedMap[TypedKey, Any]) = base
}

case class MemberNotFoundExpr(val actualType: Type, val expectedMember: Symbol) 
extends ErrorExpression {
  
}